package controller;

import view.GameJPanel;
import view.LoginJFrame;
import view.RegisterJFrame;
import view.UI;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.util.Random;


public class ActionLis implements ActionListener {
    public String strB = "";
    public String str;
    public String text;




    @Override
    public void actionPerformed(ActionEvent e) {
        String ac = e.getActionCommand();
        switch (ac) {

            case "login":
                Login();
                break;
            case "goRegister":
                UI.registerJFrame.setVisible(true);
                UI.loginJFrame.setVisible(false);
                UI.gameJFrame.setVisible(false);
                break;
            case "yesRegister":
                register();
                break;
            case "JMenu":
                UI.loginJFrame.setVisible(true);
                UI.registerJFrame.setVisible(false);
                UI.gameJFrame.setVisible(false);
                break;
            case "退出":
                GameJPanel.cardLayout.show(GameJPanel.cardPanel, "游戏界面");
                break;
            case "确定":
                UI.gameJFrame.setVisible(false);
                break;
            case "取消":
                GameJPanel.cardLayout.show(GameJPanel.cardPanel, "游戏界面");
                break;
        }
    }


    public int Login() {
        int res = 0;
        String text =  LoginJFrame.loginJPanel.usernameField.getText();
        String checkTeck=LoginJFrame.loginJPanel.checkField.getText();
        char[] acc = LoginJFrame.loginJPanel.passwordField.getPassword();
        String str = new String(acc);
        try {
            String url = "jdbc:mysql://127.0.0.1:3306/jx230821?useUnicode=true&characterEncoding=utf8&useSSL=false";
            Connection myConn = null;

            myConn = DriverManager.getConnection(url, "root", "root");
            String getSql1 = "select userName from userData where userName = '" + text + "' AND userPassword = '" + str + "'";
            Statement stam1 = myConn.createStatement();
            ResultSet re = stam1.executeQuery(getSql1);
            if (re.next() && checkTrue(checkTeck)){
                JOptionPane.showMessageDialog(null, "登入成功");
                UI.loginJFrame.setVisible(false);
                UI.registerJFrame.setVisible(false);
                UI.gameJFrame.setVisible(true);
                res = 1;
            } else if (text.isEmpty() || str.isEmpty()) {
                JOptionPane.showMessageDialog(null, "用户名或密码不能为空");
                UI.loginJFrame.setVisible(true);
                UI.gameJFrame.setVisible(false);
                UI.registerJFrame.setVisible(false);
                res = 0;
            } else if (checkFales(checkTeck)) {
                JOptionPane.showMessageDialog(null, "验证码错误，请重新输入");
                UI.loginJFrame.setVisible(true);
                UI.gameJFrame.setVisible(false);
                UI.registerJFrame.setVisible(false);
                res = 0;

            } else {
                JOptionPane.showMessageDialog(null, "账号或密码登入错误");
                UI.loginJFrame.setVisible(true);
                UI.gameJFrame.setVisible(false);
                UI.registerJFrame.setVisible(false);
                res = 0;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }

    public int register() {
        String userName = RegisterJFrame.registerJPanel.usernameField.getText();
        char[] password = RegisterJFrame.registerJPanel.passwordField.getPassword();
        String passwordS = new String(password);
        char[] passwordAgain = RegisterJFrame.registerJPanel.passwordFieldAgain.getPassword();
        String passwordSAgain = new String(passwordAgain);
        int res = 0;
        try {
            if (!passwordS.equals(passwordSAgain)) {
                JOptionPane.showMessageDialog(null, "密码输入错误");
                res = 0;
            }
            String url = "jdbc:mysql://127.0.0.1:3306/jx230821?useUnicode=true&characterEncoding=utf8&useSSL=false";
            Connection myConn1 = DriverManager.getConnection(url, "root", "root");
            String getSql = "select userName from userData where userName='" + userName + "'and userPassword ='" + passwordS + "'";
            Statement stam = myConn1.createStatement();
            ResultSet re = stam.executeQuery(getSql);
            if (userName.isEmpty() || passwordS.isEmpty()) {
                JOptionPane.showMessageDialog(null, "用户名或密码不能为空");
                UI.loginJFrame.setVisible(false);
                //游戏界面的显示
                return 0;
            } else if (re.next()) {
                JOptionPane.showMessageDialog(null, "账号重复，请重新注册");
                UI.registerJFrame.setVisible(true);
                UI.loginJFrame.setVisible(false);
                //游戏界面的显示
                return 0;
            }
            //注册账号
            String sql = "insert into userData(userName,userPassword) values('" + userName + "','" + passwordS + "')";
            int ans = stam.executeUpdate(sql);
            if (ans == 1) {
                JOptionPane.showMessageDialog(null, "注册成功");
                //游戏界面的判断
                UI.registerJFrame.setVisible(true);
                UI.loginJFrame.setVisible(false);
                res = 1;
            } else {
                JOptionPane.showMessageDialog(null, "注册失败");
                UI.registerJFrame.setVisible(true);
                //游戏界面的判断
                UI.loginJFrame.setVisible(false);
                res = 0;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }

    //验证码的生成
    public String checkCount() {
        Random ran = new Random();
        strB = "";
        String number = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        for (int i = 0; i < 4; i++) {
            int index = ran.nextInt(number.length());
            char c = number.charAt(index);
            strB += c;

        }
        try {
            String url = "jdbc:mysql://127.0.0.1:3306/jx230821?useUnicode=true&characterEncoding=utf8&useSSL=false";

            Connection myConn = DriverManager.getConnection(url, "root", "512880cai");
            String insertSql = "INSERT INTO code (code) VALUES (?)";

            PreparedStatement insertStatement = myConn.prepareStatement(insertSql);
            insertStatement.setString(1, strB);
            insertStatement.executeUpdate();


        } catch (Exception e) {
            e.printStackTrace();
        }
        return strB;
    }

    //验证码的比对
    public boolean checkTrue(String userInput) {
        String Code = strB;
        boolean match=userInput.equalsIgnoreCase(Code);
        return match;
    }

    public boolean checkFales(String userInput) {
        String Code = strB;
        boolean match=!userInput.equalsIgnoreCase(Code);
            return match;
        }
    }



